package data

import (
	"context"
	"platform-sastay-file/internal/biz"

	"github.com/go-kratos/kratos/v2/log"
)

type nodeRepo struct {
	data *Data
	log  *log.Helper
}

// NewNodeRepo .
func NewNodeRepo(data *Data, logger log.Logger) biz.NodeRepo {
	return &nodeRepo{
		data: data,
		log:  log.NewHelper(log.With(logger, "module", "repo/node")),
	}
}

func (n *nodeRepo) GetNodesByName(ctx context.Context, name string) ([]string, error) {
	service, err := n.data.registry.GetService(ctx, name)
	if err != nil {
		return nil, err
	}
	if len(service) == 0 {
		return nil, biz.ErrNodeNotFound
	}
	nodes := make([]string, 0, len(service))
	for i := range service {
		endpoints := service[i].Endpoints
		if len(endpoints) > 0 {
			nodes = append(nodes, endpoints...)
		}
	}
	return nodes, nil
}
